package container.iteraetor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestListIterator
{
    public static void main (String[] args)
    {
        List<String> arrayList = new ArrayList<>();
        
        arrayList.add("chen");
        arrayList.add("oldlu");
        arrayList.add("zhang");
        arrayList.add("li");
        
        //方式一，在迭代器中用while循环获取元素
        Iterator<String> iterator = arrayList.iterator();
        while(iterator.hasNext())
        {
            //在循环中只用写一次next()，当多次使用next()时，会报错，指针多次移动导致元素无法被找到
            String next = iterator.next();
            System.out.println(next);
        }
        
        //方式二，在迭代器中用for循环获取元素
        for(Iterator<String> iterator1 = arrayList.iterator(); iterator1.hasNext();)
        {
            String next1 = iterator1.next();
            System.out.println(next1);
        }
    }
}
